﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TradeSea;
using System.IO;
using Microsoft.Xna.Framework;

using Lidgren.Network;

namespace TradeSea
{
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    public class CommandAttribute : Attribute
    {
        public string cmdName;
        public string cmdHelp;


        // This is a positional argument
        public CommandAttribute(string cmdName, string cmdHelp)
        {
            this.cmdName = cmdName;
            this.cmdHelp = cmdHelp;
        }
    }

    class Commands
    {
        [Command("save", "Save Game")]
        public static void Save(DebugConsole console, string[] args)
        {
            //savs the name of the playlist in a variable and allows the user to select any file, txt files, or csv files.
            string fullPath =  console.Game.GetAppDir() + "\\Debug" + "\\" + console.Game.playerName + ".txt";

            //initiates the try method
            
                //creates the stream writer
                StreamWriter writer;
                //creates a file at the specified path
                writer = File.CreateText(fullPath);
                //for each line in the list, write the contents to the file
                List<string> list = console.Game.playerInventory.PrintInvintoryWOamount();
                foreach (string s in list)
                    writer.WriteLine(s);
                //closes the file
                writer.Close();
           
            
            console.Print("Game Saved", Color.Green);
        }



        [Command("help", "Provides help for console")]
        public static void Help(DebugConsole console, string[] args)
        {
            foreach (CommandAttribute item in console.Commands.Keys)
            {
                console.Print(item.cmdName.PadRight(10) + item.cmdHelp, Color.Green);
            }
        }

        [Command("commands", "Lists commands")]
        public static void Cmd(DebugConsole console, string[] args)
        {
            int i = 1;
            foreach (CommandAttribute item in console.Commands.Keys)
            {
                console.Print(i.ToString() + ")" + "  " + item.cmdName, Color.Green);
                i++;
            }
        }

        [Command("controls", "Provides control scheme")]
        public static void Control(DebugConsole console, string[] args)
        {
            console.Print("Action".PadRight(30) + "Key", Color.Green);
            console.Print(" ", Color.White);
            console.Print("Port Turn".PadRight(30) + "A", Color.Green);
            console.Print("Starboard Turn".PadRight(30) + "D", Color.Green);
            console.Print("Sails to Starboard Side".PadRight(30) + "Q", Color.Green);
            console.Print("Sails to Port Side".PadRight(30) + "W", Color.Green);
            console.Print("Begin Fishing".PadRight(30) + "F", Color.Green);
            console.Print("", Color.White);
            console.Print("3rd Person Camera".PadRight(30), Color.Green);
            console.Print("", Color.White);
            console.Print("Camera Up".PadRight(30) + "Up", Color.Green);
            console.Print("Camera Down".PadRight(30) + "Down", Color.Green);
            console.Print("Camera Left".PadRight(30) + "Left", Color.Green);
            console.Print("Camera Right".PadRight(30) + "Right", Color.Green);
            console.Print("", Color.White);
            console.Print("1st Person Camera".PadRight(30), Color.Green);
            console.Print("", Color.White);
            console.Print("Change Camera View".PadRight(30) + "Mouse", Color.Green);
            console.Print("", Color.White);
            console.Print("Free Camera".PadRight(30), Color.Green);
            console.Print("", Color.White);
            console.Print("Change Camera View".PadRight(30) + "Mouse", Color.Green);
            console.Print("", Color.White);
            console.Print("Switch View to 1st Person".PadRight(30) + "2", Color.Green);
            console.Print("Switch View to 3rd Person".PadRight(30) + "1", Color.Green);
            console.Print("Switch Free Camera".PadRight(30) + "3", Color.Green);
            console.Print("", Color.White);
            console.Print("Menu".PadRight(30) + "F12", Color.Green);
        }
        
        [Command("inventory", "Displays personal inventory")]
        public static void Inventory(DebugConsole console, string[] args)
        {
            try
            {
                List<string> list = console.Game.playerInventory.PrintInventory();
                foreach (string s in list)
                    console.Print(s, Color.Red);
            }
            catch(Exception ex)
            {
                console.Print(ex.Message, Color.Red);
            }

        }


        [Command("network", "Shows network status")]
        public static void Network(DebugConsole console, string[] args)
        {

            string network = TradeSeaNetClient.Singilton.Client.Port.ToString();
            string clientstatus = TradeSeaNetClient.Singilton.Client.Status.ToString();
            string address = TradeSeaNetClient.Singilton.Client.ServerConnection.RemoteEndpoint.Address.ToString();
            console.Print("Client : " + address + " : " + network, Color.CornflowerBlue);
            if(TradeSeaShared.Singiltons.IsListen)
            {
                console.Print(clientstatus + " as server", Color.CornflowerBlue);
            }
            else
            {
                console.Print(clientstatus + " as client", Color.CornflowerBlue);
            }
            // close enough
        }

        [Command("chat", "Sends a Chat Message")]
        public static void Chat(DebugConsole console, string[] args)
        {
            NetOutgoingMessage msg = TradeSeaNetClient.Singilton.CreateMessage(1);
            string str = "";
            for (int i = 0; i < args.Length; i++)
            {
                str += args[i] + " ";
            }
            msg.Write(str);
            TradeSeaNetClient.Singilton.SendMessage(msg, null);
        }


        [Command("tutorial", "How To.")]
        public static void Tutorial(DebugConsole console, string[] args)
        {
            //Hopefully I can cover everything in the space given...
            console.Print("Ahoy landlubber and welcome t'Tradesea! Th' newest sail'n privateer.", Color.Green);
            console.Print("I shall quickly expl'n its basics.", Color.Green);
            console.Print("", Color.White);
            console.Print("T-urn th' sails t' catch th' wind, wi' Q an' W.", Color.Green);
            console.Print("R-un aground on an isle t' pillage an' resupply.", Color.Green);
            console.Print("A-lign th' needles on th' compass an' windvane t' be off at great speed.", Color.Green);
            console.Print("D-on't lose sight o' other ships. Parley or 'ave 'im walk th' plank.", Color.Green);
            console.Print("E-r steer'n be done by turn'n us'n th' keys A an' D, port an' starboard.", Color.Green);
            console.Print("S-ight o' inventory is essential to not being broke.", Color.Green);
            console.Print("E-ven th' we 'merchants' need ta fish sometimes", Color.Green);
            console.Print("A-void rocks like scurvy and keep watch for land-ho.", Color.Green);
            console.Print("", Color.White);
            console.Print("Keep your plunder close, an' yer crew closer.", Color.Green);
            console.Print("Take everything, give nothing back!", Color.Green);
            //Arrrrrrrrrrrr. We need a voice o'er that plays when bring'n this aloft. ¡) <---look he has an eyepatch...
        }
        
        [Command("shop", "Opens shop")]
        public static void Shop(DebugConsole console, string[] args)
        {
            console.Game.shop.UpdateShop();
        }
        
        [Command("exit", "Exit's the Game")]
        public static void Exit(DebugConsole console, string[] args)
        {
            console.Print("Exiting", Color.Black);
            console.Game.Exit();
        }
    }
}
